package geektime.spring.springbucks.waiterservice;

import geektime.spring.springbucks.waiterservice.support.MoneyReadConverter;
import geektime.spring.springbucks.waiterservice.support.MoneyWriteConverter;
import io.r2dbc.h2.H2ConnectionConfiguration;
import io.r2dbc.h2.H2ConnectionFactory;
import io.r2dbc.spi.ConnectionFactory;
import java.util.Arrays;
import java.util.TimeZone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.convert.CustomConversions.StoreConversions;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import org.springframework.data.r2dbc.dialect.Dialect;
import org.springframework.data.r2dbc.function.convert.R2dbcCustomConversions;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;

@SpringBootApplication
@EnableR2dbcRepositories
public class WaiterServiceApplication extends AbstractR2dbcConfiguration {

  public static void main(String[] args) {
    SpringApplication.run(WaiterServiceApplication.class, args);
  }


  @Bean
  @Override
  public ConnectionFactory connectionFactory() {
    return new H2ConnectionFactory(
        H2ConnectionConfiguration.builder().inMemory("testdb").username("sa").build());
  }

  @Bean
  @Override
  public R2dbcCustomConversions r2dbcCustomConversions() {
    Dialect dialect = getDialect(connectionFactory());
    StoreConversions storeConversions = StoreConversions.of(dialect.getSimpleTypeHolder());

    return new R2dbcCustomConversions(storeConversions,
        Arrays.asList(new MoneyReadConverter(), new MoneyWriteConverter()));
  }

  @Bean
  public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    return builder -> builder.indentOutput(true).timeZone(TimeZone.getTimeZone("Asia/Shanghai"));
  }

}
